﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;

namespace CorrectAddinFile
{
   class Program
   {
      /// <summary>
      /// This program is used to do the following work:
      ///   Correct the Assembly node in .addin file, it should indicate the installed binary file.
      /// 
      /// it inputs two arguments:
      /// 1. The path of the .addin file which needs to be modified.
      /// 2. The directory path for the installed addon binary files.
      /// 
      /// </summary>
      /// <param name="args"></param>
      static void Main(string[] args)
      {
         try
         {
            if (args.Length != 2)
               throw new InvalidProgramException("the program arguments are incorrect.");
            string addinFileName = args[1].Trim();
            string installedFolderName = args[0].Trim();

            string replacedIndicator = "[InstalledFolder]";

            if (!File.Exists(addinFileName))
            {
                return;
            }

            StreamReader reader = new StreamReader(addinFileName);
            string fileContent = reader.ReadToEnd();
            Encoding currentEncoding = reader.CurrentEncoding;
            reader.Close();

            fileContent = fileContent.Replace(replacedIndicator, installedFolderName);

            StreamWriter writer = new StreamWriter(addinFileName, false, currentEncoding);
            writer.Write(fileContent);
            writer.Flush();
            writer.Close();
         }
         catch(Exception ex)
         {
            Console.WriteLine(ex.ToString());
         }
      }
   }
}
